package com.scaffold.catalog.action;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.scaffold.BasicBizAction;
import com.scaffold.catalog.entity.Catalog;
import com.scaffold.catalog.service.CatalogService;

@Controller
public class CatalogJsonAction extends BasicBizAction{
	@Autowired
	private CatalogService catalogService;
	
	@RequestMapping("/catalog/save.json")
	public String save(Long id){
		if(id==null){
			Catalog catalog = new Catalog();
			this.bindParams(catalog);
			this.setOwner(catalog);
			this.catalogService.save(catalog);
		}else{
			Catalog catalog = this.catalogService.getCatalog(id);
			this.checkOwner(catalog);
			this.bindParams(catalog);
			this.catalogService.update(catalog);
		}
		return this.getResult("0", null, null);
	}
	
	@RequestMapping("/catalog/get.json")
	public String get(Long id){
		Catalog catalog = catalogService.getCatalog(id);
		this.checkOwner(catalog);
		return this.getResult("0", null, catalog);
	}

	@RequestMapping("/catalog/delete.json")
	public String delete(Long id){
		Catalog catalog = catalogService.getCatalog(id);
		this.checkOwner(catalog);
		this.catalogService.delete(catalog);
		return this.getResult("0", null, null);
	}
	
	@RequestMapping("/catalog/select.json")
	public String select(Long parentId){
		if(parentId==null){
			parentId = 0L;
		}
		List<Catalog> list = catalogService.findByParentId(parentId);
		return this.getResult("0", null, list);
	}
	
}
